package com.itheimaoa.web.action.process;

import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.ServletActionContext;
import org.jbpm.api.task.Task;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.itheimaoa.domain.privilege.Employee;
import com.itheimaoa.domain.process.Audition;
import com.itheimaoa.service.process.IApprovalService;
import com.itheimaoa.service.process.IAuditionService;
import com.itheimaoa.web.form.process.AuditionForm;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

@Controller
@Scope("prototype")
public class AuditionAction extends ActionSupport implements ModelDriven<AuditionForm> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private AuditionForm form = new AuditionForm();
	@Resource
	private IAuditionService auditionService;
	@Resource
	private IApprovalService approvalService;

	public String list() {
		Employee employee = (Employee) ServletActionContext.getRequest().getSession().getAttribute("employee");
		Map<Audition, Task> rtMap = approvalService.listAudition(employee.getEid().toString());
		ActionContext.getContext().put("rtMap", rtMap);
		return "list";
	}

	/**
	 * 审批页面
	 * 
	 * @return
	 */
	public String approval() {
		return "approval";
	}

	/**
	 * 审批
	 * 
	 * @return
	 */
	public String doApproval() {
		Employee employee = (Employee) ServletActionContext.getRequest().getSession().getAttribute("employee");
		Audition audition = auditionService.findEntity(form.getAid());
		audition.setComment(form.getComment());
		audition.setAgree(form.getAgree());
		audition.setScore(form.getScore());
		approvalService.doAuditionApproval(employee, audition, form.getTaskId());
		ActionContext.getContext().put("message", "审批完成!");
		ActionContext.getContext().put("urladdress", "/itheimaoa/control/process/auditionlist.do");
		return "message";
	}

	/**
	 * 下载
	 * 
	 * @return
	 */
	public String download() {
		return "download";
	}

	@Override
	public AuditionForm getModel() {
		return form;
	}
}
